Page({
    data: {
        unlockType: 1,//解锁方式
        questionId: 0,//题库id
        baseSet:{},//基础显示
        unlockText: [
            '分享朋友圈、群、好友，永久解锁',
            '',
            '可通过兑换码、支付，永久解锁',
            '可通过兑换码、支付，永久解锁',
            '分享朋友圈、群、好友，永久解锁'
        ],//解锁标题
        showMask: false,//是否显失败示弹窗
        showLockMask: false,//是否解锁
        showCodeMask: false,//是否解锁
        questionInfo: {},//题库详情
        code:'',//兑换码
        timer:0,
    },
    onShareAppMessage: function (e) {
        console.log(e)
        console.log(`/pages/index/index?pid=${wx.$cache.get('member_id')}${e.target.dataset.flag==1?'&question_id='+this.data.questionId:0}`)
        return {
            title: wx.$cache.get('fx').title,
            imageUrl: wx.$cache.get('fx').img,
            path: `/pages/index/index?pid=${wx.$cache.get('member_id')}${e.target.dataset.flag==1?'&question_id='+this.data.questionId:0}`
        }
    },
    onShareTimeline() {
        return {
            title: wx.$cache.get('fx').title,
            imageUrl: wx.$cache.get('fx').img,
            query: `a=${wx.$cache.get('member_id')}`,
        }
    },
    onHide(){
      clearInterval(this.data.timer)
    },
    onLoad:async function (options) {
        let timer = setInterval(()=>{
            this.get_mk_question_info();
        },10000)
        this.setData({
            questionId: options.id || 0,
            baseSet:await wx.$getBase(),
            timer
        }, () => {
            this.get_mk_question_info();
        })

    },
    //购买题库
    buy(){
        const _this = this;
        wx.$request(wx.$api.mk_pay_question,{
            question_id: this.data.questionId
        }).then(res=>{
            wx.requestPayment({
                ...res,
                success(res) {
                    _this.setData({
                        showLockMask: true
                    })
                },
                fail(res) {
                    wx.showToast({
                        title:'支付失败',
                        icon:'none'
                    })
                }
            })
        })
    },
    //分享

    //图片预览
    previewImg(){
      wx.previewImage({
          urls:[this.data.baseSet.mk_qrcode]
      })
    },
    //获取题库详情
    get_mk_question_info() {
        wx.$request(wx.$api.mk_question_info, {
            question_id: this.data.questionId
        }).then(res => {
            res.question_info.examination_s = parseInt(res.question_info.examination_s/60)

            if (res.question_info.type===1){
                let end_time = new Date(res.question_info.end_time.replace(/-/g,'/'))
                res.question_info.end_time_str = `${res.question_info.end_time} - ${String(end_time.getHours()).length === 1 ? '0' + end_time.getHours() : end_time.getHours()}:${String(end_time.getMinutes()).length === 1 ? '0' + end_time.getMinutes() : end_time.getMinutes()}`

            }else res.question_info.end_time_str = '无限期'
               /* if (res.share_log.state===2){
                    this.setData({
                        showLockMask:true
                    })
                }*/
            //分享截图
          /*  if (res.share_log.state===2){
                this.setData({
                    showLockMask:true
                })
            }else if (res.share_log.state===3){
                this.setData({
                    showMask:true
                })
            }*/

res.question_info.on_line=parseInt(res.question_info.on_line/35)
            this.setData({
                questionInfo: res,
                unlockType:res.question_info.charge_type
            })
        })
    },
    //打开兑换码弹窗
    openCodeMask() {
        this.setData({
            showCodeMask: true
        })
    },
    //激活码输入
    codeInput(e){
        console.log(e)
        const {value} = e.detail;
        this.setData({
            code:value
        })
    },
    //关闭激活码弹窗-确定输入
    sureCode(){
        if (!this.data.code) return wx.showToast({
            title:'请输入兑换码',
            icon:'none'
        })
        this.mk_to_exchange()

    },
    //使用兑换码
    mk_to_exchange(){
      wx.$request(wx.$api.mk_to_exchange,{
          question_id:this.data.questionId,
          code:this.data.code
      }).then(res=>{

          this.get_mk_question_info();
          this.setData({
              showCodeMask:false,
              showLockMask:true
          })
      })
    },
    //关闭激活码弹窗
    closeCodeMask() {
        this.setData({
            showCodeMask: false
        })
    },
    //关闭解锁弹窗
    closeLockMask() {
        this.setData({
            showLockMask: false
        })
        wx.navigateBack()
    },
    //打开弹窗
    openMask() {
        this.setData({
            showMask: true
        })
    },
    //关闭弹窗
    closeMask() {
        this.setData({
            showMask: false
        })
    },
    //跳转
    jumpPage(e) {
        this.setData({
            showMask: false
        })
        wx.navigateTo({
            url: e.currentTarget.dataset.path
        })
    },
});
